<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实名认证 - 房屋租赁平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 500px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: transform 0.2s ease;
        }

        .btn:hover {
            transform: translateY(-2px);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .alert {
            padding: 12px 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .info-box {
            background-color: #e7f3ff;
            border: 1px solid #b3d9ff;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
        }

        .info-box h3 {
            color: #0066cc;
            margin-bottom: 10px;
            font-size: 16px;
        }

        .info-box ul {
            color: #333;
            font-size: 14px;
            margin-left: 20px;
        }

        .info-box li {
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔐 实名认证</h1>
            <p>为了保障您的账户安全，请完成实名认证</p>
        </div>

        <div class="info-box">
            <h3>📋 认证须知</h3>
            <ul>
                <li>请确保填写的姓名与身份证上的姓名完全一致</li>
                <li>身份证号码将用于验证您的身份信息</li>
                <li>认证信息将被加密保存，仅用于身份验证</li>
                <li>完成认证后才能使用房东、中介等高级功能</li>
            </ul>
        </div>

        <div id="alertContainer"></div>

        <form id="identityForm">
            <div class="form-group">
                <label for="realName">真实姓名 *</label>
                <input type="text" id="realName" name="realName" placeholder="请输入您的真实姓名" required>
            </div>

            <div class="form-group">
                <label for="identityCard">身份证号码 *</label>
                <input type="text" id="identityCard" name="identityCard" placeholder="请输入18位身份证号码" required>
            </div>

            <button type="submit" class="btn" id="submitBtn">
                <span id="btnText">提交认证</span>
            </button>
        </form>
    </div>

    <script>
        // 获取token（实际应用中应该从localStorage或cookie中获取）
        function getToken() {
            return localStorage.getItem('userToken') || 'your-test-token-here';
        }

        // 显示提示信息
        function showAlert(message, type = 'error') {
            const alertContainer = document.getElementById('alertContainer');
            const alertDiv = document.createElement('div');
            alertDiv.className = `alert alert-${type}`;
            alertDiv.textContent = message;
            
            alertContainer.innerHTML = '';
            alertContainer.appendChild(alertDiv);
            
            // 3秒后自动隐藏
            setTimeout(() => {
                alertDiv.remove();
            }, 3000);
        }

        // 验证真实姓名（中文）
        function validateRealName(name) {
            const chineseRegex = /^[\u4e00-\u9fa5]+$/;
            return chineseRegex.test(name);
        }

        // 验证身份证号码
        function validateIdCard(idCard) {
            const idCardRegex = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
            return idCardRegex.test(idCard);
        }

        // 提交实名认证
        async function submitIdentityVerification(realName, identityCard) {
            const token = getToken();
            
            try {
                const response = await fetch('/user/verify-identity', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'info': token
                    },
                    body: JSON.stringify({
                        realName: realName,
                        identityCard: identityCard
                    })
                });

                const result = await response.json();
                
                if (response.ok && result.code === 200) {
                    showAlert('实名认证成功！', 'success');
                    // 可以在这里跳转到其他页面
                    setTimeout(() => {
                        window.location.href = '/user/profile';
                    }, 2000);
                } else {
                    showAlert(result.message || '认证失败，请重试');
                }
            } catch (error) {
                console.error('认证请求失败:', error);
                showAlert('网络错误，请检查网络连接后重试');
            }
        }

        // 表单提交处理
        document.getElementById('identityForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const realName = document.getElementById('realName').value.trim();
            const identityCard = document.getElementById('identityCard').value.trim();
            const submitBtn = document.getElementById('submitBtn');
            const btnText = document.getElementById('btnText');
            
            // 验证输入
            if (!realName) {
                showAlert('请输入真实姓名');
                return;
            }
            
            if (!validateRealName(realName)) {
                showAlert('真实姓名必须为中文');
                return;
            }
            
            if (!identityCard) {
                showAlert('请输入身份证号码');
                return;
            }
            
            if (!validateIdCard(identityCard)) {
                showAlert('身份证号码格式不正确');
                return;
            }
            
            // 禁用提交按钮
            submitBtn.disabled = true;
            btnText.textContent = '认证中...';
            
            try {
                await submitIdentityVerification(realName, identityCard);
            } finally {
                // 恢复提交按钮
                submitBtn.disabled = false;
                btnText.textContent = '提交认证';
            }
        });

        // 页面加载时检查登录状态
        window.addEventListener('load', function() {
            const token = getToken();
            if (!token || token === 'your-test-token-here') {
                showAlert('请先登录后再进行实名认证');
            }
        });
    </script>
</body>
</html>
